package com.flot.yazb;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.support.annotation.StringRes;
import android.view.inputmethod.InputMethodManager;

import com.flot.yazb.loading.CLoadingDialog;

import java.util.HashMap;
import java.util.Map;

public class CUIAgent {

    public static void fixFontScale(Context context) {
        Configuration mCurConfig = context.getResources().getConfiguration();
        mCurConfig.fontScale = 1f;
        context.getResources().updateConfiguration(mCurConfig, context.getResources().getDisplayMetrics());
    }

    private static Map<String, CLoadingDialog> sProgressDialogCache = new HashMap<>();

    public static void clearCache(Activity activity) {
        if (sProgressDialogCache.containsKey(activity.toString())) {
            CLoadingDialog dialog = getProgressDialog(activity);

            if (dialog.isShowing())
                dialog.dismiss();

            sProgressDialogCache.remove(activity.toString());
        }
    }


    private static CLoadingDialog getProgressDialog(Activity activity) {
        CLoadingDialog dialog = sProgressDialogCache.get(activity.toString());
        if (dialog == null) {
            dialog = new CLoadingDialog.Builder(activity).build();
            sProgressDialogCache.put(activity.toString(), dialog);
        }

        return dialog;
    }

    public static void showProgress(Activity activity) {
        if (activity == null || activity.isFinishing())
            return;

        CLoadingDialog dialog = getProgressDialog(activity);

        if (!dialog.isShowing())
            dialog.show();
    }

    public static void dismissProgress(Activity activity) {
        if (activity == null || activity.isFinishing())
            return;

        CLoadingDialog dialog = getProgressDialog(activity);

        if (dialog.isShowing())
            dialog.dismiss();
    }

    public static void showToast(Activity activity, String text) {
        if (activity == null || activity.isFinishing())
            return;
    }

    public static void showToast(Activity activity, @StringRes int textRes) {
        if (activity == null || activity.isFinishing())
            return;
    }

    public static void hideSoftInput(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (null == imm)
            return;
        imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(),
                0);
    }
}
